import { Controller, Post, BodyParam, Param, UploadedFile, Get } from 'routing-controllers';
import { uploadFile, downloadFile } from '../service/file.service';
import { message } from '../utils/result';
import { Code } from '../utils/code';

@Controller('/file')
export class FileController {
  @Post('/upload')
  async upload(@BodyParam('uid') uid: string, @UploadedFile('file') file: any) {
    console.log(file);
    const isUploaded = await uploadFile(uid, file);
    if (isUploaded) {
      return message(Code.SUCCESS, '上传文件成功');
    }
    return message(Code.ERROR_FILE_UPLOAD, '上传文件失败');
  }

  @Get('/download/:filename')
  async download(@Param('filename') filename: string) {
    const file = await downloadFile(filename);
    if (file) return file;
    return message(Code.ERROR_FILE_DOWNLOAD, 'Not Found');
  }
}
